//
//  MineMoneyTreeViewController.m
//  Tongcheng
//
//  Created by 邢广范 on 2017/4/10.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//

#import "MineMoneyTreeViewController.h"
#import "MineMoneyOperator.h"
#import "MineMoneyModel.h"
#import "MineMoneyDataModel.h"
#import "MineMoneyListModel.h"
#import "MineMoneyTableViewCell.h"
#import "MJRefresh.h"

@interface MineMoneyTreeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *moneyBtn;
- (IBAction)moneyBtnClick:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;

@end

@implementation MineMoneyTreeViewController

static NSString *moneyCellIdentifier = @"MineMoneyTableViewCell";

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

- (NSInteger)page
{
    if (!_page) {
        _page = 1;
    }
    return _page;
}

- (MJRefreshAutoNormalFooter *)footer
{
    if (_footer == nil) {
        _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downloadMore)];
        
    }
    return _footer;
}

- (void)setupView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self download];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self download];
    // Do any additional setup after loading the view from its nib.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineMoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:moneyCellIdentifier];
    if (!cell) {
        cell = [[UINib nibWithNibName:@"MineMoneyTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    MineMoneyListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Type"] = @"0";
    dic[@"Page"] = [NSString stringWithFormat:@"%ld", self.page];
    dic[@"Row"] = @"30";
    MineMoneyOperator *operator = [[MineMoneyOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.tableView.mj_header endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataArray = operator.data.data.list;
            if (self.dataArray.count < 30) {
                self.tableView.mj_footer = nil;
            }else{
                self.page ++;
                self.tableView.mj_footer = self.footer;
            }
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)downloadMore
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Type"] = @"0";
    dic[@"Page"] = [NSString stringWithFormat:@"%ld", self.page];
    dic[@"Row"] = @"30";
    MineMoneyOperator *operator = [[MineMoneyOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.tableView.mj_footer endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            NSMutableArray *array = [NSMutableArray arrayWithArray:self.dataArray];
            [array addObjectsFromArray:operator.data.data.list];
            self.dataArray = array;
            if (operator.data.data.list.count < 30) {
                self.tableView.mj_footer = nil;
            }else{
                self.page ++;
                self.tableView.mj_footer = self.footer;
            }
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)moneyBtnClick:(id)sender {
}
@end
